/*
 * groupchatMsgCatcher.java
 *
 * Created on 3 Октябрь 2007 г., 16:09
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xmppbot;

import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.muc.MultiUserChat;
import xmppbot.parse.options;
import xmppbot.parse.utils;

/**
 *
 * @author lian
 */
public class groupMsgCatcher implements PacketListener
{
    public String NickName;
    public XMPPConnection connect;
    public MultiUserChat mchat;
    
    /** Creates a new instance of groupchatMsgCatcher */
    public groupMsgCatcher (String NickName, XMPPConnection connect, MultiUserChat mchat)
    {
        this.NickName = NickName;
        this.connect = connect;
        this.mchat = mchat;
    }

    public void processPacket (Packet packet)
    {
        boolean b = true; // Simple initialisation
        String NickName1 = NickName + ":";
        String NickName2 = NickName + ",";
        if (((Message) packet).getBody ().length () > NickName.length ())
        {
            String tmp = ((Message) packet).getBody ().substring (0, NickName.length () + 1);
            if (NickName1.equalsIgnoreCase (tmp) | NickName2.equalsIgnoreCase (tmp))
            {
                b = false;
            }
        }
        else
        {
            b = true;
        }
        
        
        if (b)
        {
            Date date = (Date) new GregorianCalendar().getTime().clone();
            String MsgType = "groupchat";
            String Msg = ((Message) packet).getBody();
            String From = packet.getFrom();
            String ShortFrom = StringUtils.parseResource(packet.getFrom());
            options opts = new options(date, MsgType, Msg, From, ShortFrom, packet, connect, mchat);
            try {
                utils.creatPacket(opts);
            } catch (SQLException ex) {
                ex.printStackTrace();
            } catch (XMPPException ex1) {
                ex1.printStackTrace();
            }
        }
        else
        {
            Date date = (Date) new GregorianCalendar().getTime().clone();
            String MsgType = "gprivate";
            String Msg = ((Message) packet).getBody ().substring (NickName.length () + 2, ((Message) packet).getBody ().length ());
            String From = packet.getFrom();
            String ShortFrom = StringUtils.parseResource(packet.getFrom());
            options opts = new options(date, MsgType, Msg, From, ShortFrom, packet, connect, mchat);
            try {
                utils.creatPacket(opts);
            } catch (SQLException ex) {
                ex.printStackTrace();
            } catch (XMPPException ex1) {
                ex1.printStackTrace();
            }
        }
    }
    
}
